package com.cxb;

import com.cxb.view.DialogCreater;

import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.view.ViewGroup;
import android.widget.TextView;

public class DeviceInfoActivityK extends BaseActivity implements
		OnTouchListener {
	private View v_versioninfo, v_gpstest, v_resume , ib_back;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_deviceinfo);
		initView();
	}

	private void initView() {
		v_versioninfo = this.findViewById(R.id.v_versioninfo);
		v_gpstest = this.findViewById(R.id.v_gpstest);
		v_resume = this.findViewById(R.id.v_resume);
		ib_back = this.findViewById(R.id.ib_back);
		ib_back.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				DeviceInfoActivityK.this.finish();
			}
		});
		v_resume.setOnTouchListener(this);
		v_gpstest.setOnTouchListener(this);
		v_versioninfo.setOnTouchListener(this);
	}

	long touchtime = 0;

	@Override
	public boolean onTouch(View v, MotionEvent event) {

		if (event.getAction() == MotionEvent.ACTION_DOWN) {
			touchtime = System.currentTimeMillis();
			touchItem((ViewGroup)v);
		}
		if (event.getAction() == MotionEvent.ACTION_UP) {
			if (System.currentTimeMillis() - touchtime < 800) {
				click(v);
			}
			releasedItem((ViewGroup)v);
		}

		return true;
	}

	private void click(View view) {
		switch (view.getId()) {
		case R.id.v_versioninfo:
			gotoAcitivity(DeviceInfoActivity.class);
			break;
		case R.id.v_gpstest:
			gotoAcitivity(GpsTestActivity.class);
			break;
		case R.id.v_resume:
			DialogCreater dc = new DialogCreater(this);
			final Dialog d = dc.getCommonDialog(0, R.string.recovery);
			TextView tv_confirm = (TextView) dc.getView(R.id.tv_confirm);
			TextView tv_cancel = (TextView) dc.getView(R.id.tv_cancel);
			tv_confirm.setOnClickListener(new OnClickListener() {
				
				@Override
				public void onClick(View v) {
					recovery();
					d.dismiss();
				}
			});
			tv_cancel.setOnClickListener(new OnClickListener() {
				
				@Override
				public void onClick(View v) {
					d.dismiss();
				}
			});
			d.show();
			break;
		}
	}
	

	private void recovery() {
		sendBroadcast(new Intent("android.intent.action.MASTER_CLEAR"));
	}

	
	private void gotoAcitivity(Class activity)
	{
		Intent intent = new Intent();
		intent.setClass(this, activity);
		this.startActivity(intent);
	}
	
	private void touchItem(ViewGroup viewGroup)
	{
		TextView textView = (TextView)viewGroup.getChildAt(0);
		textView.setTextColor(this.getResources().getColor(R.color.blueline));
		View view = viewGroup.getChildAt(1);
		view.setBackgroundResource(R.drawable.goto_press);
		viewGroup.setBackgroundColor(0xff666666);
	}
	
	private void releasedItem(ViewGroup viewGroup)
	{
		TextView textView = (TextView)viewGroup.getChildAt(0);
		textView.setTextColor(this.getResources().getColor(R.color.text_normal_color));
		View view = viewGroup.getChildAt(1);
		view.setBackgroundResource(R.drawable.goto_n);
		viewGroup.setBackgroundColor(0x00cccccc);
	}

}
